import {
  FileAndFolderNodeMessage,
  FileMessage,
  FolderMessage,
  getChildFolderList,
} from '@/api/tree-folder';

// 根据id从tree结构中获取节点
export function getNodeById(
  nodes: FileAndFolderNodeMessage[],
  id: string
): FileAndFolderNodeMessage | null {
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i].id === id) return nodes[i];
    const children = nodes[i].children || [];
    if (children?.length > 0) {
      const val = getNodeById(children, id);
      if (val) return val;
    }
  }
  return null;
}

// 名称重复校验
export async function isFolderRepeated(
  currentFile: FileMessage,
  value: string
) {
  const childrenFolderRes = await getChildFolderList(
    currentFile.projectId,
    currentFile.teamId,
    currentFile.type,
    currentFile.id
  );
  const childrenFolder: FolderMessage[] = childrenFolderRes.data.list;
  const repeated = childrenFolder.findIndex((item) => item.name === value);
  return repeated !== -1;
}
